package com.iscc.maoke.controller;

import com.google.gson.Gson;
import com.iscc.maoke.domain.Type;
import com.iscc.maoke.service.TypeService;
import com.iscc.maoke.service.impl.TypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet({"/query_all_type.do", "/add_type.do", "/del_type.do", "/update_type.do", "/query_type_id.do"})
public class TypeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        Gson gson = new Gson();

        String uri = request.getRequestURI();
        String action = uri.substring(uri.lastIndexOf("/") + 1);
        TypeService typeService = new TypeServiceImpl();
        Map<String, Object> resultMap = new HashMap<>();
        if ("query_all_type.do".equals(action)) {
            String name = request.getParameter("tname");
            List<Type> types = typeService.queryAllTypeByCondition(name);
            if (types.size() > 0) {
                resultMap.put("code", 200);
                resultMap.put("msg", "查询数据成功");
                resultMap.put("result", types);
            } else {
                resultMap.put("code", 201);
                resultMap.put("msg", "暂无类别数据");
            }
        } else if ("add_type.do".equals(action)) {
            String name = request.getParameter("tname");
            Type type = new Type(name);
            int result = typeService.addType(type);
            if (result != -1) {
                resultMap.put("code", 200);
                resultMap.put("msg", "添加类别成功");
            } else {
                resultMap.put("code", 201);
                resultMap.put("msg", "添加类别失败");
            }
        } else if ("del_type.do".equals(action)) {
            int tno = Integer.parseInt(request.getParameter("tno"));
            int result = typeService.delType(tno);
            if (result != -1) {
                resultMap.put("code", 200);
                resultMap.put("msg", "删除类别成功");
            } else {
                resultMap.put("code", 201);
                resultMap.put("msg", "删除类别失败");
            }
        } else if ("query_type_id.do".equals(action)) {
            int tno = Integer.parseInt(request.getParameter("tno"));
            Type type = typeService.queryTypeById(tno);
            if (type != null) {
                resultMap.put("code", 200);
                resultMap.put("msg", "查询类别成功");
                resultMap.put("result", type);
            } else {
                resultMap.put("code", 201);
                resultMap.put("msg", "没有该类别信息");
            }
        } else if ("update_type.do".equals(action)) {
            int tno = Integer.parseInt(request.getParameter("tno"));
            String tname = request.getParameter("tname");
            Type type = new Type(tno, tname);
            int result = typeService.updateType(type);
            if (result != -1) {
                resultMap.put("code", 200);
                resultMap.put("msg", "更新类别成功");
            } else {
                resultMap.put("code", 201);
                resultMap.put("msg", "更新类别失败");
            }
        }

        String resultStr = gson.toJson(resultMap);
        out.print(resultStr);

    }
}
